﻿<?php

    /**
      *  Author:yf
      * 使用说明:微信公众号无限群发接口，使用实例:  
      *  $test = new SendAllMsg("你的appId","你的appSecret");
      *  $test->sendMsgToAll(); //调用群发方法
      *  注：1.使用条件：认证号或测试号
      *    2.群发消息内容可为图文、文本、音乐等,$data具体内容参照微信开发文档/客服接口
      *    3.若用户量过万,需修改getUserInfo()，具体参照信开发文档/获取关注者列表
      *
     */

class sendAllMsg {
    private $access_token;
    
    public function __construct($access_token) {
        $this->access_token = $access_token;
    }
    
    public function getData($url){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
        curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        $data = curl_exec($ch);
        curl_close($ch);
        return $data;        
    }
    
    public function postData($url,$data){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $tmpInfo = curl_exec($ch);
        if (curl_errno($ch)) {
            return curl_error($ch);
        }
        curl_close($ch);
        return $tmpInfo;
    }
    
    private function getUserInfo(){
        $url = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=".$this->access_token;
        $res = $this->getData($url);
        $jres = json_decode($res,true);
        $userInfoList = $jres['data']['openid'];
        return $userInfoList;
    }
    
    
    //群发
    function sendMsgToAll($sdata){
        set_time_limit(0);
        $userInfoList = $this->getUserInfo();
        $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$this->access_token;
         foreach($userInfoList as $val){
            $data='
            {
                "touser":"'.$val.'",
                "msgtype":"news",
                "news":{
                    "articles": '.$sdata.'
                }
            }    
                ';
           $rs=$this->postData($url,$data);
			}
            return $rs;
    }
	
	
	//发给个人
	/*
	oY4SHwEgnzMrldGtIi2nTs9RENGQ  安迪andy
	oY4SHwGdtDsNui0ZbL4Oe8o7PZCU  曾赞
	oY4SHwGJxkqf2QxNwVDLvTjSVIV0  许泽天
	oY4SHwH0-gJWUhBNwKdocIPnaHnQ  零下五度
	*/
	function sendMsgToOne($sdata,$openid){
        set_time_limit(0);
        $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$this->access_token;
            $data='
            {
                "touser":"'.$openid.'",
                "msgtype":"news",
                "news":{
                    "articles": '.$sdata.'
                }
            }    
                ';
           $rs=$this->postData($url,$data);
            return $rs;
    }

	
}
 
?>